package com.webshooter.math;

public class Vector {

    public float x;
    public float y;

    public Vector() {
        this.x = 0;
        this.y = 0;
    }

    public Vector(Vector vector) {
        this.x = vector.x;
        this.y = vector.y;
    }

    public Vector(float x, float y) {
        this.x = x;
        this.y = y;
    }

    public Vector normalizeTo(float targetNorm) {
        Vector normVector = new Vector();
        double norm = Math.sqrt(x * x + y * y);
        normVector.x = Math.round(x * targetNorm / norm);
        normVector.y = Math.round(y * targetNorm / norm);
        return normVector;
    }

    public float norm() {
        return (float) Math.sqrt(x * x + y * y);
    }

    public float normSquared() {
        return x * x + y * y;
    }

    public Vector add(Vector vector) {
        Vector sum = new Vector();
        sum.x = x + vector.x;
        sum.y = y + vector.y;
        return sum;
    }

    public Vector subtract(Vector vector) {
        Vector sum = new Vector();
        sum.x = x - vector.x;
        sum.y = y - vector.y;
        return sum;
    }

    public Vector multiply(float f) {
        return new Vector(Math.round(x * f), Math.round(y * f));
    }

    public float dotProduct(Vector v) {
        return x * v.x + y * v.y;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 23 * hash + Float.floatToIntBits(this.x);
        hash = 23 * hash + Float.floatToIntBits(this.y);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Vector other = (Vector) obj;
        if (this.x != other.x) {
            return false;
        }
        if (this.y != other.y) {
            return false;
        }
        return true;
    }
}
